package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.FactoryData;
import com.ruoyi.factorydata.domain.ProductionLineData;
import com.ruoyi.factorydata.domain.dto.WorkShopDataDTO;
import com.ruoyi.factorydata.domain.vo.WorkshopDataVo;
import com.ruoyi.factorydata.mapper.FactoryDataMapper;
import com.ruoyi.factorydata.mapper.ProductionLineDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.factorydata.mapper.WorkshopDataMapper;
import com.ruoyi.factorydata.domain.WorkshopData;
import com.ruoyi.factorydata.service.IWorkshopDataService;

/**
 * 车间数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@Service
public class WorkshopDataServiceImpl extends ServiceImpl<WorkshopDataMapper, WorkshopData> implements IWorkshopDataService {
    @Autowired
    private WorkshopDataMapper workshopDataMapper;

    @Autowired
    private ProductionLineDataMapper productionLineDataMapper;

    @Autowired
    private FactoryDataMapper factoryDataMapper;

    /**
     * 查询车间数据
     *
     * @param id 车间数据主键
     * @return 车间数据
     */
    @Override
    public WorkshopData selectWorkshopDataById(Long id) {
        return workshopDataMapper.selectWorkshopDataById(id);
    }

    /**
     * 查询车间数据列表
     *
     * @param workShopDataDTO 车间数据
     * @return 车间数据
     */
    @Override
    public List<WorkshopDataVo> selectWorkshopDataList(WorkShopDataDTO workShopDataDTO) {
//        ProductionLineData productionLineData = new ProductionLineData();
//        if (workshopData != null && workshopData.getId() != null){
//            productionLineData.setWorkshopId(workshopData.getId());
//        }
//        List<ProductionLineData> productionLineDataList = productionLineDataMapper.selectProductionLineDataList(productionLineData);
//        List<WorkshopDataVo> workshopDataVoList = productionLineDataList.stream().map(productionLine -> {
//            WorkshopData workshopDataById = workshopDataMapper.selectWorkshopDataById(productionLine.getWorkshopId());
//            FactoryData factoryDataById = factoryDataMapper.selectFactoryDataById(workshopDataById.getFactoryId());
//            return WorkshopDataVo.builder()
//                    .id(workshopDataById.getId())
//                    .name(workshopDataById.getName())
//                    .workshopCode(workshopDataById.getWorkshopCode())
//                    .factoryName(factoryDataById.getFactoryName())
//                    .factoryId(factoryDataById.getId())
//                    .remarks(workshopDataById.getRemarks())
//                    .delFlag(workshopDataById.getDelFlag())
//                    .version(workshopDataById.getVersion())
//                    .productionLineCode(productionLine.getProductionLineCode())
//                    .productionLineName(productionLine.getName())
//                    .productionLineId(productionLine.getId())
//                    .createBy(productionLine.getCreateBy())
//                    .createTime(productionLine.getCreateTime())
//                    .updateTime(productionLine.getUpdateTime());
//        }).collect(Collectors.toList()).stream().map(WorkshopDataVo.WorkshopDataVoBuilder::build).collect(Collectors.toList());
        WorkshopData workshopData = new WorkshopData();
        workshopData.setName(workShopDataDTO.getName());
        workshopData.setWorkshopCode(workShopDataDTO.getWorkshopCode());
        workshopData.setFactoryId(workShopDataDTO.getFactoryId());
        List<WorkshopData> workshopDataList = workshopDataMapper.selectWorkshopDataList(workshopData);
        List<WorkshopDataVo> workshopDataVoList = workshopDataList.stream().map(workshop -> {
            FactoryData factoryDataById = factoryDataMapper.selectFactoryDataById(workshop.getFactoryId());
            return WorkshopDataVo.builder()
                    .id(workshop.getId())
                    .name(workshop.getName())
                    .workshopCode(workshop.getWorkshopCode())
                    .factoryId(workshop.getFactoryId())
                    .factoryName(factoryDataById.getFactoryName())
                    .remarks(workshop.getRemarks())
                    .delFlag(workshop.getDelFlag())
                    .version(workshop.getVersion())
                    .createBy(workshop.getCreateBy())
                    .createTime(workshop.getCreateTime())
                    .updateTime(workshop.getUpdateTime());
        }).map(WorkshopDataVo.WorkshopDataVoBuilder::build).collect(Collectors.toList());
        return workshopDataVoList;
    }

    /**
     * 新增车间数据
     *
     * @param workshopData 车间数据
     * @return 结果
     */
    @Override
    public int insertWorkshopData(WorkshopData workshopData) {
                workshopData.setCreateTime(DateUtils.getNowDate());
            return workshopDataMapper.insertWorkshopData(workshopData);
    }

    /**
     * 修改车间数据
     *
     * @param workshopData 车间数据
     * @return 结果
     */
    @Override
    public int updateWorkshopData(WorkshopData workshopData) {
                workshopData.setUpdateTime(DateUtils.getNowDate());
        return workshopDataMapper.updateWorkshopData(workshopData);
    }

    /**
     * 批量删除车间数据
     *
     * @param ids 需要删除的车间数据主键
     * @return 结果
     */
    @Override
    public int deleteWorkshopDataByIds(Long[] ids) {
        int i = workshopDataMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        return i;
    }

    /**
     * 删除车间数据信息
     *
     * @param id 车间数据主键
     * @return 结果
     */
    @Override
    public int deleteWorkshopDataById(Long id) {
        return workshopDataMapper.deleteWorkshopDataById(id);
    }
}